var xmlHttp; //定义XMLHttpRequest对象
function createXmlHttpRequestObject() {
    //如果在internet Explorer下运行
    if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlHttp = false;
        }

    } else {
        //如果在Mozilla或其他的浏览器下运行
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            xmlHttp = false;
        }
    }
    //返回创建的对象或显示错误信息
    if (!xmlHttp)
        alert("返回创建的对象或显示错误信息");
    else
        return xmlHttp;
}

//加购
function add_cart(type) {
    createXmlHttpRequestObject();

    var goods_id = document.getElementById("goods_id").value;
    var uid = document.getElementById("uid").value;
    var num = document.getElementById("num").value;
    var name = document.getElementById("name").value;
    var phone = document.getElementById("phone").value;
    var address = document.getElementById("address").value;

    //检查地址输入不为空
    //除去空格
    address = address.replace(/\s+/g, "");
    if (address == "") {
        alert("请输入收货地址！");
        return false;
    }

    //检查姓名输入不为空
    //除去空格
    name = name.replace(/\s+/g, "");
    if (name == "") {
        alert("请输入收货人姓名！");
        return false;
    }

    //检查手机号输入不为空
    if (phone == "") {
        alert("请输入收货人手机号！");
        return false;
    }

    // 检查手机号格式正确
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
        alert("手机格式号码有误！");
        return false;
    }

    //检查购买数量是否大于0
    if (num <= 0) {
        alert("购买数量必须大于0！");
        return false;
    }

    // 转换为JSON数据
    var data_list = {
        'goods_id': goods_id,
        'uid': uid,
        'num': num,
        'name': name,
        'phone': phone,
        'address': address
    }
    data_list = JSON.stringify(data_list);

    xmlHttp.onreadystatechange = get_add_action; //判断URL调用的状态值并处理
    xmlHttp.open("GET", 'php/add_cart.php?i=' + data_list, false);
    xmlHttp.send(null);
}

function get_add_action() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        if (xmlHttp.responseText == 1) {
            alert("添加成功！");
        } else {
            alert("添加失败！");
        }
    }
}


//发表评论
function add_remark() {
    var remark = document.getElementById("remark").value;
    //除去空格
    remark = remark.replace(/\s+/g, "");
    if (remark == "") {
        alert("评论内容不能为空！");
        return false;
    }
}

//提交订单
function submit_order() {
    //检查地址输入不为空
    var address = document.getElementById("address").value;
    //除去空格
    address = address.replace(/\s+/g, "");
    if (address == "") {
        alert("请输入收货地址！");
        return false;
    }

    //检查姓名输入不为空
    var name = document.getElementById("name").value;
    //除去空格
    name = name.replace(/\s+/g, "");
    if (name == "") {
        alert("请输入收货人姓名！");
        return false;
    }

    //检查手机号输入不为空
    if (document.getElementById("phone").value == "") {
        alert("请输入收货人手机号！");
        return false;
    }

    // 检查手机号格式正确
    var phone = document.getElementById("phone").value;
    if (!(/^1[3456789]\d{9}$/.test(phone))) {
        alert("手机格式号码有误！");
        return false;
    }

    //检查购买数量是否大于0
    var num = document.getElementById("num").value;
    if (num <= 0) {
        alert("购买数量必须大于0！");
        return false;
    }
}

function get_pay() {
    var num = document.getElementById("num").value;
    num = parseInt(num);//将购买数量转换为整数
    var price = document.getElementById("price").value;
    price = price.substring(0, price.length - 1); //去掉价格中的"元"
    price = parseFloat(price); //将价格转换为浮点数
    document.getElementById("pay").value = num * price + '元';
}

function count_price() {
    var num = document.getElementById("num").value;
    num = parseInt(num);//将购买数量转换为整数
    if (num <= 0) {
        alert("购买数量必须大于0！");
        document.getElementById("num").value = 1;
        get_pay();
    }
    if (num > 10) {
        alert("单次购买数量不可大于10！");
        document.getElementById("num").value = 10;
        get_pay();
    }
    var g_num = document.getElementById("goods_num").value;
    if (num > g_num) {
        alert("购买数量不可大于库存！");
        document.getElementById("num").value = 1;
        get_pay();
    }
    get_pay();
}